package com.lin.sms.web.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/randomCode")
public class RandomCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//生成随机数
		String randomCode = UUID.randomUUID().toString().substring(0,4);
		//System.out.println(randomCode);
		//把随机数放入Seesion中
		req.getSession().setAttribute("RANDOMCODE_IN_SESSION", randomCode);
		
		//创建图片对象
		int width = 80;
		int height = 30;
		int imageType = BufferedImage.TYPE_INT_BGR;
		BufferedImage image = new BufferedImage(width, height, imageType);
		
		//画板
		Graphics g = image.getGraphics();
		g.setColor(Color.LIGHT_GRAY);
		//绘制一个实心矩形
		g.fillRect(1, 1, width -2, height-2);
		
		//把随机数画进图片中
		g.setColor(Color.BLACK);//设置随机数颜色
		Font font = new Font("宋体",Font.BOLD + Font.ITALIC, 20);//定义字体
		g.setFont(font);//设置随机数字体和大小
		g.drawString(randomCode, 10, 25);
		//干扰线
		g.setColor(Color.GRAY);
		Random r = new Random();
		for (int i = 0; i < 100; i++) {
			g.fillRect(r.nextInt(width), r.nextInt(height), 2, 2);
		}

		//关闭
		g.dispose();
		//把图片对象以流的方式保存出去
		ImageIO.write(image, "jpg", resp.getOutputStream());
	}
}
